package com.android.measuringimportanceoflectureslides.activities;


import com.android.measuringimportanceoflectureslides.R;
import com.android.measuringimportanceoflectureslides.Camera.CrosshairOverlay;
import com.android.measuringimportanceoflectureslides.Camera.PreviewView;
import com.android.measuringimportanceoflectureslides.Camera.ReadSlideFrame;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Surface;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;


public class PreviewActivity extends Activity {

	private int mNumberOfCameras;
	private int mDefaultCameraId;
	private int nCameras;
	private Camera mCamera;
	private PreviewView mPreview;
	private int mCameraCurrentlyLocked;
    private int mnCameraOrientation;
    private ReadSlideFrame mReadSlideFrame;
    
	protected void onPause() {
		super.onPause();
		if (mCamera != null) {
			mPreview.setCamera(null);
			mCamera.autoFocus(null);
			mCamera.setPreviewCallback(null);
			mCamera.release();
			mCamera = null;
		}
	}

	protected void onRestart() {
		super.onRestart();
	}

	protected void onResume() {
		super.onResume();

		mCamera = Camera.open(mCameraCurrentlyLocked);
		mPreview.setCamera(mCamera);
		mCamera.setPreviewCallback(mReadSlideFrame);
		
		String focusMode = mCamera.getParameters().getFocusMode();
		
        if (focusMode.equals(Camera.Parameters.FOCUS_MODE_AUTO)
                || focusMode.equals(Camera.Parameters.FOCUS_MODE_MACRO))
        {
            //original code from the book
//            mCamera.autoFocus(mReadBarcode);
            //delayed focusing works on more devices
        	mReadSlideFrame.autoFocusLater(mCamera);
        }
        else
        {
        }
	}

	protected void onStart() {
		super.onStart();
	}

	protected void onStop() {
		super.onStop();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.camera_preview_surface_holder);
		
		
		TextView tvBarcode = (TextView) findViewById(R.id.textView1);
        CheckBox ckSuccess = (CheckBox) findViewById(R.id.checkBox1);
        
        // Find the total number of cameras available
        int numberOfCameras = Camera.getNumberOfCameras();
        
        CrosshairOverlay co = (CrosshairOverlay) findViewById(R.id.crosshairoverlay1);
        
		mPreview = (PreviewView) findViewById(R.id.camera_preview_surface_holder_xml_previewView);
		
		// set the position where we expect the barcodes to appear
        // 0.5 = middle of the screen
		mReadSlideFrame = new ReadSlideFrame(0.5d, tvBarcode, ckSuccess, co);
        
		mNumberOfCameras = Camera.getNumberOfCameras();
		
		// Find the ID of the default camera
		CameraInfo cameraInfo = new CameraInfo();
		
		for (int i = 0; i < mNumberOfCameras; i++) {
			Camera.getCameraInfo(i, cameraInfo);
			if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
				mDefaultCameraId = i;
			}
		}
		if (mDefaultCameraId == -1) {
			// test for no cameras
			if (nCameras > 0) {
				mDefaultCameraId = 0;
			} else {
				// nothing can be done; tell the user then exit
				Toast toast = Toast.makeText(getApplicationContext(),
						"No Camera", Toast.LENGTH_LONG);
				toast.show();
				finish();
			}
		}
		mCameraCurrentlyLocked = mDefaultCameraId;
	}
	
	public void setCameraDisplayOrientation()
    {
        CameraInfo cameraInfo = new CameraInfo();
        Camera.getCameraInfo(mCameraCurrentlyLocked, cameraInfo);
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation)
        {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
        {
            mnCameraOrientation = (cameraInfo.orientation + degrees) % 360;
            mnCameraOrientation = (360 - mnCameraOrientation) % 360; // compensate
                                                                     // the
                                                                     // mirror
        } else
        { // back-facing
            mnCameraOrientation = (cameraInfo.orientation - degrees + 360) % 360;
        }
        mCamera.setDisplayOrientation(mnCameraOrientation);
    }


	
}
